/*
 * DoublyLinkedList.cpp
 *
 *  Created on: 2012-04-04
 *      Author: tony
 */

#include "DoublyLinkedList.h"

namespace graph {

DoublyLinkedList::DoublyLinkedList() {
	std::cout << "        DoublyLinkedList constructor" << std::endl;

	first = NULL;
}

DoublyLinkedList::~DoublyLinkedList() {
	std::cout << "        DoublyLinkedList destructor" << std::endl;
	DLNode* temp = first;
	while (temp!=NULL){
		first = temp->getNext();
		delete temp;
		temp = first;
	}
}

void DoublyLinkedList::addEdge(const int i, const int j, const float w){
	std::cout << "         DoublyLinkedList addEdge - " << i << " " << j << " " << w << std::endl;
	first = new DoublyLinkedList::DLNode( i, j, w, first, NULL);
	if ( first->getNext() != NULL)
		first->getNext()->setPrev( first );
}

void DoublyLinkedList::removeEdge(const int i, const int j){
	std::cout << "         DoublyLinkedList removeEdge - " << i << " " << j << std::endl;
	DoublyLinkedList::DLNode* temp = first;
	while (temp != NULL){
		if (temp->getEnd() == j){
			// rewire list and remove temp
			if ( temp->getNext() != NULL )
				temp->getNext()->setPrev( temp->getPrev() );
			if (temp->getPrev() == NULL)
				first = temp->getNext();
			else
				temp->getPrev()->setNext( temp->getNext() );

			delete temp;
			return;
		}
		temp = temp->getNext();
	}
}

} /* namespace graph */
